package ftp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.ftpserver.impl.DefaultFtpServer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class FtpServerListener implements ServletContextListener {
	public static String FTPSERVER_CONTEXT_NAME = "FTPSERVER_CONTEXT_NAME";

	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("Stopping FtpServer");
		DefaultFtpServer server = (DefaultFtpServer) sce.getServletContext().getAttribute(FTPSERVER_CONTEXT_NAME);
		if (server != null) {
			server.stop();
			sce.getServletContext().removeAttribute("FTPSERVER_CONTEXT_NAME");
			System.out.println("FtpServer stopped");
		} else {
			System.out.println("No running FtpServer found");
		}
	}

	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("Starting FtpServer");
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		DefaultFtpServer server = (DefaultFtpServer) ctx.getBean("myUserServer");
		sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server);
		try {
			server.start();
			System.out.println("FtpServer started");
		} catch (Exception e) {
			throw new RuntimeException("Failed to start FtpServer", e);
		}
	}
}